﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FlatRedBall;
using FlatRedBall.Graphics;
using Runaway;

namespace Runaway
{
    class GameManager
    {
        // Helpful text for programmers
        bool DEBUG_TEXT_ON = true;
        
        private Player player;
        private List<Platform> platforms;
        private List<Coin> coins;
        private List<Dog> dogs;
        private String temp = "NO";

        Text text = TextManager.AddText("text");

        public GameManager()
        {
            platforms = new List<Platform>();
            coins = new List<Coin>();
            dogs = new List<Dog>();
            
            //create platforms for testing
            for (int i = 0; i < 100; i++)
            {
                platforms.Add(new Platform((float)(0 + 1.8 *i),-0));
            }

            for (int i = 0; i < 50; i++)
            {
                platforms.Add(new Platform(0, (float)(5 + 1.8 * i)));
                platforms.Add(new Platform(10, (float)(10 + 1.8 * i)));
                platforms.Add(new Platform(25, (float)(10 + 1.8 * i)));
            }

            coins.Add(new Coin(15, 10));
            dogs.Add(new Dog(20, 5));

            player = new Player(10,5);
            
        }

        private void playerUpdate()
        {
            

            //we assume the player is in the air to start
            bool jumping = true;
            bool wallslide = false;
            
            //Detect if the player is not in the air. 
            foreach (Platform platform in platforms)
            {
                if (player.getHitBox().CollideAgainstBounce(platform.getHitBox(), 0, 1, 0) == true) 
                {
                    if (player.getHitBox().LastMoveCollisionReposition.Y > 0)
                    {
                        jumping = false;
                    }
                    else if (player.getHitBox().LastMoveCollisionReposition.X != 0)
                    {
                        wallslide = true;
                    }
             
                }
            }

            if (jumping == true)
            {
                player.changeState(Player.state.Jumping);
            }
            if (wallslide == true)
            {
                player.changeState(Player.state.WallSliding);
            }


            foreach (Coin coin in coins)
            {
                if (player.getHitBox().CollideAgainst(coin.getHitBox()) == true)
                {
                    player.getMoney(coin.getValue());
                    //coins.Remove(coin);
                    coin.Destroy(); //MUHAHAHAHAHAH
                }
            }
            //if the player isn't in the air then detect how they are moving. 
            if (jumping == false)
            {
                if (player.getState() != Player.state.Attacking)
                {
                    if (Input.getMoveLeftInput() == true || Input.getMoveRightInput() == true)
                    {
                        if (Input.getSlidingInput() == true)
                        {
                            player.changeState(Player.state.Sliding);
                        }
                        else
                        {
                            player.changeState(Player.state.Running);
                        }
                    }
                    else
                    {
                        player.changeState(Player.state.Standing);
                    }
                }
                
            }


            if (DEBUG_TEXT_ON) Debug_Text();
            

            player.Update();
        }


        private void dogUpdate()
        {
            foreach (Dog dog in dogs)
            {
                foreach (Platform platform in platforms)
                    dog.getHitBox().CollideAgainstBounce(platform.getHitBox(), 0, 1, 0);
                
                if (dog.detectPlayer(player.X, player.Y) == Dog.playerDetect.Right)
                    dog.setMoveDirection(Dog.moveDirection.Right);
                else if(dog.detectPlayer(player.X, player.Y) == Dog.playerDetect.Left)
                    dog.setMoveDirection(Dog.moveDirection.Left);

                dog.Update();
            }

        }

        public void Update()
        {
            playerUpdate();
            dogUpdate();
        }

        public void Debug_Text()
        {
            text.X = player.Position.X;
            text.Y = player.Position.Y + 10;
            text.DisplayText = "Xvelocity: " + player.Velocity.X.ToString();
            text.DisplayText += "\nYvelocity: " + player.Velocity.Y.ToString();
            text.DisplayText += "\nState: ";
            
            switch (player.getState())
            {
                case Player.state.Attacking:
                    text.DisplayText += "ATTACKING";
                    break;
                case Player.state.Jumping:
                    text.DisplayText += "JUMPING";
                    break;
                case Player.state.Running:
                    text.DisplayText += "RUNNING";
                    break;
                case Player.state.Sliding:
                    text.DisplayText += "SLIDING";
                    break;
                case Player.state.Standing:
                    text.DisplayText += "STANDING";
                    break;
                case Player.state.WallSliding: ;
                    text.DisplayText += "WALLSLIDING";
                    break;
            }

            /*text.DisplayText += "\nHitbox Collision: ";
            
            foreach (Platform platform in platforms)
            {
                if (player.getHitBox().CollideAgainstMove(platform.getHitBox(),0,1))
                    if(player.getHitBox().LastMoveCollisionReposition.Y > 0)
                    {
                        text.DisplayText += player.getHitBox().LastMoveCollisionReposition.Y;
                        temp = "YES";
                    }

                //if (player.getHitBox().
            }

            text.DisplayText += "\nRecollision Y: ";
            //text.DisplayText += player.getHitBox().LastMoveCollisionReposition.Y;
            text.DisplayText += temp;
             */
      
        }
        

        public void Destroy()
        {
            player.Destroy();
            foreach (Dog dog in dogs)
            {
                dog.Destroy();
            }
        }

    }
}
